package com.ys.controller.web.hr;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.service.hr.TwlStockBorrowService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.hr.TwlStockBorrow;
import com.ys.entity.hr.TwlStockBorrowDetail;
import com.ys.entity.sys.TsUserChange;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/hr/twlStockBorrow")
public class WebTwlStockBorrowController {
	@Autowired
	private CommonApiService<TwlStockBorrow> commonApiService;

	@Autowired
	private TwlStockBorrowService twlStockBorrowService;


	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){
		List<Map<String, Object>> sonlist = new ArrayList<>();
		
		// 子列表配置信息
		SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TWL_StockBorrow, sonlist);
		try {
			String bid = commonApiService.getCommonNextBid(MenuId.TWL_StockBorrow);

			TwlStockBorrow entity = new TwlStockBorrow()
					.setBid(bid)
					.setCreateTime(new Date());

			return commonApiService.toAdd(model, entity, MenuId.TWL_StockBorrow, null, subGridConfig);


		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 审核
	 */
	@RequiresPermissions("twlStockBorrow:add")
	@GetMapping("/toFlow")
	public String toFlow(Model model, HttpServletRequest request, HttpServletResponse response,
			String bid){
		if (bid == null || bid.length() == 0) {
			return W.error(model, "单号不能为空");
		}
		
		TwlStockBorrow entity = new TwlStockBorrow().setBid(bid).queryFirst();
		
		Map<String, Object> extraParam = new HashMap<String, Object>();
		Long borrowChangeId = entity.getBorrowChangeId();
		TsUserChange tsUserChange = new TsUserChange().setId(borrowChangeId).queryFirst();
		extraParam.put("borrowUserId", tsUserChange.getUserId());
		extraParam.put("borrowOrgName", tsUserChange.getDeptName());
		
		try {
			// 子列表配置信息
			List<Map<String, Object>> sonlist = twlStockBorrowService.getItemList(bid);
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TWL_StockBorrow, sonlist);
			return commonApiService.toEdit(model, entity.getId(), new TwlStockBorrow(), MenuId.TWL_StockBorrow, extraParam, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, "数据有误，请联系客服");
		}
	}
}